package com.beidouapp.security.mvc.view;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

public class Pdfview extends AbstractIText5PdfView
{
	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		System.out.println(model.get("test"));
		Paragraph header = new Paragraph(new Chunk("PDF 输出测试", getChineseFont(24)));
		document.add(header);
		document.add(new Paragraph("测试生成本地文件", getChineseFont(12)));
	}
	

	private static final Font getChineseFont(float size)
	{
		Font FontChinese = null;
		try
		{
			BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			FontChinese = new Font(bfChinese, size, Font.NORMAL);
		}
		catch (DocumentException de)
		{
			System.err.println(de.getMessage());
		}
		catch (IOException ioe)
		{
			System.err.println(ioe.getMessage());
		}
		return FontChinese;
	}

	public void fillData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException
	{
		for (String key : data.keySet())
		{
			String value = data.get(key);
			fields.setField(key, value);
		}
	}

	public Map<String, String> data()
	{
		Map<String, String> data = new HashMap<String, String>();
		data.put("borrower", "胡桃同学");
		return data;
	}
	public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        // step 3
        document.open();
        // step 4
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(""), Charset.forName("UTF-8"));
        // step 5
        
        document.close();
    }
}
